import {extend} from "umi-request";
import { message} from 'antd';
import {history} from "@@/core/history";
import {stringify} from "querystring";


console.log(process.env.NODE_ENV);

const request = extend({
  credentials: 'include', // 默认请求是否带上cookie
  prefix: process.env.NODE_ENV === 'production' ? 'http://1.116.132.238:8080' : undefined,
});

request.interceptors.response.use(async (response): Promise<any> => {
  const res = await response.clone().json();
  if (res.code === 0) {
    return res.data;
  } else if(res.code === 400100){
    message.error(res.msg);
    // 如果没有登录，重定向到 login
    history.replace({
      pathname: '/user/login',
      search: stringify({
        redirect: location.pathname,
      })
    })
  }else{
    message.error(res.description);
  }
});

export default request;
